<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
        <!--部门数据-->
        <pane size="16">
          <el-col>
            <div class="head-container">
              <el-input v-model="typeName"
                        placeholder="请输入类型名称"
                        clearable
                        prefix-icon="Search" style="margin-bottom: 20px"/>

              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                      type="primary"
                      plain
                      icon="Plus"
                      @click="handleTypeAdd"
                      v-hasPermi="['biz:bizPwdType:add']"
                  >新增
                  </el-button>
                </el-col>
              </el-row>
            </div>
            <div class="head-container">
              <el-tree :data="bizPwdTypeList"
                       v-loading="loadingType"
                       :props="{ label: 'name', children: 'children' }"
                       :expand-on-click-node="false"
                       :filter-node-method="filterNode"
                       ref="typeTreeRef" node-key="id"
                       highlight-current
                       @node-click="handleNodeClick">
                <template #default="{ node, data }">
                  <span class="custom-tree-node">
                    <span>{{ node.label }}</span>
                    <span v-if="data.id">
                      <el-button link type="primary" icon="Edit" @click="() => handleTypeUpdate(data)"></el-button>
                      <el-button link type="primary" icon="Plus" @click="() => handleTypeAdd(data)"></el-button>
                      <el-button link type="primary" icon="Delete" @click="() => handleTypeDelete(data)"></el-button>
                    </span>
                  </span>
                </template>
              </el-tree>
            </div>
          </el-col>
        </pane>
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
              <el-row>
                <el-col :xs="12" :sm="8" :md="6" :lg="6" :xl="6">
                  <el-form-item label="单位名称" prop="companyName">
                    <el-input maxlength="50"
                              class="search-width-custom"
                              v-model="queryParams.companyName"
                              placeholder="请输入单位名称"
                              clearable
                              @keyup.enter="handleQuery"
                    />
                  </el-form-item>
                </el-col>
                <el-col :xs="12" :sm="8" :md="6" :lg="6" :xl="6">
                  <el-form-item label="姓名" prop="name">
                    <el-input maxlength="50"
                              class="search-width-custom"
                              v-model="queryParams.name"
                              placeholder="请输入姓名"
                              clearable
                              @keyup.enter="handleQuery"
                    />
                  </el-form-item>
                </el-col>
                <el-col :xs="12" :sm="8" :md="6" :lg="6" :xl="6">
                  <el-form-item label="电话" prop="phone">
                    <el-input maxlength="50"
                              class="search-width-custom"
                              v-model="queryParams.phone"
                              placeholder="请输入电话"
                              clearable
                              @keyup.enter="handleQuery"
                    />
                  </el-form-item>
                </el-col>
                <el-col :xs="12" :sm="8" :md="6" :lg="6" :xl="6">
                  <el-form-item label="邮箱" prop="email">
                    <el-input maxlength="50"
                              class="search-width-custom"
                              v-model="queryParams.email"
                              placeholder="请输入邮箱"
                              clearable
                              @keyup.enter="handleQuery"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <div style="width:100%;height:40px">
              <el-form-item style="float:right">
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
              </div>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['biz:bizPwd:add']"
                >新增
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['biz:bizPwd:edit']"
                >修改
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['biz:bizPwd:remove']"
                >删除
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['biz:bizPwd:export']"
                >导出
                </el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="bizPwdList" border @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="left"/>
              <el-table-column label="序号" type="index" align="left" width="60"/>
              <el-table-column label="密码分类" align="left" prop="typeName" show-overflow-tooltip/>
              <el-table-column label="单位名称" align="left" prop="companyName" show-overflow-tooltip/>
              <el-table-column label="单位账号" align="left" prop="account" show-overflow-tooltip/>
              <el-table-column label="单位密码" align="left" prop="pwd" show-overflow-tooltip/>
              <el-table-column label="负责人账号" align="left" min-width="90" prop="managerAccount" show-overflow-tooltip/>
              <el-table-column label="负责人密码" align="left" min-width="90" prop="managerPwd" show-overflow-tooltip/>
              <el-table-column label="负责人名称" align="left" min-width="90" prop="managerName" show-overflow-tooltip/>
              <el-table-column label="负责人电话" align="left" min-width="90" prop="managerPhone" show-overflow-tooltip/>
              <el-table-column label="联系人名称" align="left" min-width="90" prop="contactsName" show-overflow-tooltip/>
              <el-table-column label="联系人电话" align="left" min-width="90" prop="contactsPhone" show-overflow-tooltip/>
              <el-table-column label="邮箱" align="left" prop="email" show-overflow-tooltip/>
              <el-table-column label="平台网址" align="left" prop="platform" show-overflow-tooltip/>
              <el-table-column label="U盾密码" align="left" prop="upwd" show-overflow-tooltip/>
              <el-table-column label="U盾有效期" align="left" min-width="120" prop="upwdValidity" show-overflow-tooltip/>
              <el-table-column label="备注" align="left" prop="memo" show-overflow-tooltip/>
              <el-table-column fixed="right" label="操作" align="left" width="150">
                <template #default="scope">
                  <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                             v-hasPermi="['biz:bizPwd:edit']">修改
                  </el-button>
                  <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                             v-hasPermi="['biz:bizPwd:remove']">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
            />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <!-- 添加或修改密码管理对话框 -->
    <el-dialog :close-on-click-modal="false" draggable :title="title" v-model="open" width="800px" append-to-body
               @close="cancel">
      <el-form ref="bizPwdRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分类" prop="typeId">
          <el-tree-select
              v-model="form.typeId"
              :data="bizPwdTypeAllList"
              :props="{ value: 'id', label: 'name', children: 'children' }"
              value-key="id"
              placeholder="请选择分类"
              check-strictly
          />
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input maxlength="200" v-model="form.companyName" placeholder="请输入单位名称"/>
        </el-form-item>
        <el-form-item label="单位账号" prop="account">
          <el-input maxlength="500" v-model="form.account" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="单位密码" prop="pwd">
          <el-input maxlength="500" v-model="form.pwd" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="负责人账号" prop="managerAccount">
          <el-input maxlength="500" v-model="form.managerAccount" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="负责人密码" prop="managerPwd">
          <el-input maxlength="500" v-model="form.managerPwd" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="负责人名称" prop="managerName">
          <el-input maxlength="50" v-model="form.managerName" placeholder="请输入负责人名称"/>
        </el-form-item>
        <el-form-item label="负责人电话" prop="managerPhone">
          <el-input maxlength="50" v-model="form.managerPhone" placeholder="请输入负责人电话"/>
        </el-form-item>
        <el-form-item label="联系人名称" prop="contactsName">
          <el-input maxlength="50" v-model="form.contactsName" placeholder="请输入联系人名称"/>
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactsPhone">
          <el-input maxlength="50" v-model="form.contactsPhone" placeholder="请输入联系人电话"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input maxlength="50" v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="平台网址" prop="platform">
          <el-input maxlength="1000" v-model="form.platform" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="U盾密码" prop="upwd">
          <el-input maxlength="1000" v-model="form.upwd" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="U盾有效期" prop="upwdValidity">
          <el-date-picker clearable
                          v-model="form.upwdValidity"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          type="datetime"
                          placeholder="请选择U盾有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input maxlength="1000" v-model="form.memo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <typeAdd ref="typeAddRef" :id="typeId" :parentId="typeParentId" @ok="getTypeList"/>
  </div>
</template>

<script setup name="BizPwd">
import {listBizPwd, getBizPwd, delBizPwd, addBizPwd, updateBizPwd} from "@/api/biz/bizPwd";
import {listBizPwdType, delBizPwdType} from "@/api/biz/bizPwdType";
import {Splitpanes, Pane} from "splitpanes"
import "splitpanes/dist/splitpanes.css"
import useAppStore from "@/store/modules/app.js";
import typeAdd from "@/views/biz/bizPwdType/add.vue";
import {currentDate} from '@/utils/dateutil';

const {proxy} = getCurrentInstance();

const appStore = useAppStore()
const bizPwdTypeAllList = ref([]);
const bizPwdTypeList = ref([]);
const bizPwdList = ref([]);
const open = ref(false);
const loading = ref(true);
const loadingType = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const typeName = ref("");
const typeId = ref(0);
const typeParentId = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    typeId: null,
    companyName: null,
    name: null,
    phone: null,
    email: null,
  },
  rules: {
    typeId: [
      {required: true, message: "分类不能为空", trigger: "submit"}
    ],
    account: [
      {required: true, message: "单位账号不能为空", trigger: "submit"}
    ],
    pwd: [
      {required: true, message: "单位密码不能为空", trigger: "submit"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询密码管理列表 */
function getList() {
  loading.value = true;
  listBizPwd(queryParams.value).then(response => {
    bizPwdList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    updateTime: null,
    deleted: null,
    typeId: null,
    companyName: null,
    account: null,
    pwd: null,
    memo: null,
    managerAccount: null,
    managerPwd: null,
    managerName: null,
    managerPhone: null,
    contactsName: null,
    contactsPhone: null,
    email: null,
    platform: null,
    upwd: null,
    upwdValidity: null
  };
  proxy.resetForm("bizPwdRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  proxy.$refs.typeTreeRef.setCurrentKey(null);
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  form.value.typeId = queryParams.value.typeId;
  open.value = true;
  title.value = "添加密码管理";
}

function handleTypeAdd(row) {
  typeId.value = null;
  typeParentId.value = row ? row.id : null;
  nextTick(() => {
    proxy.$refs["typeAddRef"].show();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBizPwd(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改密码管理";
  });
}

function handleTypeUpdate(row) {
  typeId.value = row.id;
  nextTick(() => {
    proxy.$refs["typeAddRef"].show();
  });
}

function handleTypeDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该密码分类？').then(function () {
    return delBizPwdType(_ids);
  }).then(() => {
    getTypeList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bizPwdRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBizPwd(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBizPwd(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除密码管理数据项？').then(function () {
    return delBizPwd(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('biz/bizPwd/export', {
    ...queryParams.value
  }, `密码管理_${currentDate()}.xlsx`)
}

function getTypeList() {
  loadingType.value = true;
  listBizPwdType().then(response => {
    const data = {id: '', name: '全部', children: []};
    bizPwdTypeList.value = proxy.handleTree(response.data, "id", "parentId");
    bizPwdTypeAllList.value = proxy.handleTree(response.data, "id", "parentId");
    bizPwdTypeList.value.unshift(data);
    loadingType.value = false;
    nextTick(() => {
      proxy.$refs["typeTreeRef"].setCurrentKey('')
    });
  });
}

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.name.indexOf(value) !== -1;
};

/** 节点单击事件 */
function handleNodeClick(data) {
  queryParams.value.typeId = data.id;
  handleQuery();
}

/** 根据名称筛选树 */
watch(typeName, val => {
  proxy.$refs["typeTreeRef"].filter(val);
});

getList();
getTypeList();
</script>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

::v-deep .el-collapse-item__content {
  padding: 0 !important;
}

::v-deep .el-tree {
  height: calc(100% - 82px);
  overflow: auto;
}
.search-width-custom{
  width: 15vw;
}
</style>
